package model;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;

public class ScannerTest {
    public static void main(String[] args) {
        //创建键盘输入的对象
        Scanner scanner=new Scanner(System.in);

        TreeSet<Student> treeSet=new TreeSet<Student>();

        System.out.println("你要录入几名学生？");
        //想要获得学生数量
        int count=scanner.nextInt();

        for (int i = 0; i <count ; i++) {
            scanner=new Scanner(System.in);
            System.out.println("请输入学生姓名：");
            //获得学生姓名
            String s=scanner.nextLine();
            System.out.println("请输入学生的年龄：");
            //获得学生年龄
            Integer n=scanner.nextInt();
            //创建学生对象
            Student student=new Student();
            student.setName(s);
            student.setAge(n);
            //
            //把这些学生一个一个的放到集合里
            treeSet.add(student);
        }BufferedWriter bw=null;
        //把集合里面的东西写到本地文件
        try {
            bw=new BufferedWriter(new FileWriter("student.txt"));
            for (Student s:treeSet)
            {bw.write(s.getName()+"-----"+s.getAge());
            bw.newLine();}
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bw!=null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("写入完毕");
    }



}
